package com.hdsx.restful.glzt.structure.sd.rest;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Service;

import com.hdsx.restful.base.PreparedDataListener;
import com.hdsx.restful.glzt.roadline.lxxx.bean.Znode;
import com.hdsx.restful.glzt.structure.ql.bean.StatisticalResult;
import com.hdsx.restful.glzt.structure.sd.bean.Sd;
import com.hdsx.restful.glzt.structure.sd.service.SdServer;
import com.hdsx.restful.util.CheckEmpty;
import com.hdsx.restful.util.Dao;
import com.hdsx.restful.util.EasyUI;


// TODO: Auto-generated Javadoc
/**
 * sd服务
 */
@Path("/sd")
@Service
public class SdRestServer {

	/** The sd server. */
	@Resource(name = "sdServerImpl")
	private SdServer server;

	/**
	 * Gets the list.
	 * http://192.168.0.137:8080/restful/rest/sd/list?xzqhbm=610730&sdmc=青龙垭隧道下&lxbm=G5&page=0&rows=10
	 * @param
	 * @param
	 * @return the list
	 */
	
	//综合查询
	@GET
	@Path("/list")
	@Produces(MediaType.APPLICATION_JSON)
	public EasyUI<Sd> getList(@QueryParam("lxbm") String lxbm,@QueryParam("xzqhbm")String xzqhbm,
			@QueryParam("sxxfx") String sxxfx,@QueryParam("qdzh") String qdzh_string,
			@QueryParam("zdzh") String zdzh_string,@QueryParam("name") String name,
			@QueryParam("page") int page,@QueryParam("rows") int rows) throws Exception{
		EasyUI<Sd> easyUI=new EasyUI<Sd>(page,rows);
		Double qdzh = null;
		Double zdzh = null;
		try{
			qdzh = Double.parseDouble(qdzh_string);
			zdzh = Double.parseDouble(zdzh_string);
		}catch(Exception e){
			System.out.println("qdzh和zdzh没有输入或格式错误！");qdzh=null;zdzh=null;
		}
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		int count =server.queryCount(lxbm,xzqhbm,sxxfx,qdzh,zdzh,name);
		List<Sd> results = server.queryList(lxbm,xzqhbm,sxxfx,qdzh,zdzh,name,easyUI.getBegin(),easyUI.getEnd());
		easyUI.setTotal(count);
		easyUI.setRows(results);
		return easyUI;
	}

	//按路线查询
	@GET
	@Path("/getListByLxbm")
	@Produces(MediaType.APPLICATION_JSON)
	public EasyUI<Sd> getListByLxbm(@QueryParam("lxbm") String lxbm,@QueryParam("page") int page,@QueryParam("rows") int rows){
		EasyUI<Sd> easyUI=new EasyUI<Sd>(page,rows);
		int count =server.querySdCountByLxbm(lxbm);
		List<Sd> SdListByLxbm = server.getListByLxbm(lxbm,easyUI.getBegin(),easyUI.getEnd());
		easyUI.setTotal(count);
		easyUI.setRows(SdListByLxbm);
		return easyUI;
	}
//按行政区划查询
	@GET
	@Path("/getListByXzqhbm")
	@Produces(MediaType.APPLICATION_JSON)
	public EasyUI<Sd> getListByXzqhbm(@QueryParam("xzqhbm") String xzqhbm,@QueryParam("page") int page,@QueryParam("rows") int rows){
		EasyUI<Sd> easyUI=new EasyUI<Sd>(page,rows);
		int count =server.querySdCountByXzqhbm(xzqhbm);
		List<Sd> SdListByXzqhbm = server.getListByXzqhbm(xzqhbm,easyUI.getBegin(),easyUI.getEnd());
		easyUI.setTotal(count);
		easyUI.setRows(SdListByXzqhbm);
		return easyUI;
	}
	
	@GET
	@Path("/getBySdbm")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Sd> getBySdbm(@QueryParam("sdbm") String sdbm){
		List<Sd> SdListByXzqhbm = server.getBySdbm(sdbm);
		return SdListByXzqhbm;
	}
	
	@GET
	@Path("/countSdflByXzqhbm")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Sd>  countSdflByXzqhbm(@QueryParam("xzqhbm") String xzqhbm) {
		List<Sd> list = server.countSdflByXzqhbm(xzqhbm);
		String[] arr={"其它","短隧道","中隧道","长隧道"};
		String sdfl = null;
		Set<String> keys= new HashSet<String>();
		for (Sd sd : list) {
			 sdfl = sd.getSdfl();
			keys.add(sdfl);
		}
		for (String key : arr) {
			if(!keys.contains(key))
			{
				Sd sd=new Sd();
				sd.setSdfl(key);
				sd.setSum("0");
				list.add(sd);
			}
			
		}
		return list;
	}
	
	@GET
	@Path("/countSdflByLxbm")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Sd>  countSdflByLxbm(@QueryParam("lxbm") String lxbm) {
		List<Sd> list = server.countSdflByLxbm(lxbm);
		String[] arr={"其它","短隧道","中隧道","长隧道"};
		String sdfl = null;
		Set<String> keys= new HashSet<String>();
		for (Sd sd : list) {
			 sdfl = sd.getSdfl();
			keys.add(sdfl);
		}
		for (String key : arr) {
			if(!keys.contains(key))
			{
				Sd sd=new Sd();
				sd.setSdfl(key);
				sd.setSum("0");
				list.add(sd);
			}
			
		}
		return list;
	}
	
	/**
	 * 根据id得到sd
	 * 
	 * @param id
	 *            唯一标识
	 * @return
	 */
	@GET
	@Path("/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public Sd getById(@PathParam("id") String id) {
		Sd sd = server.getById(id);
		return sd;
	}

	/**
	 * 根据id删除sd
	 * 
	 * @param id
	 *            唯一标识
	 * @return 若成功返回true，否则返回false
	 */
	@DELETE
	@Path("/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public boolean deleteById(@PathParam("id") String id) {
		return server.deleteById(id);
	}

	/**
	 * 更新sd
	 * 
	 * @param sd
	 *            待更新的sd
	 * @return 若成功返回true，否则返回false
	 */
	@POST
	@Path("/update")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public boolean updatesd(Sd sd) {

		return server.updatesd(sd);
	}

	/**
	 * 添加sd
	 * 
	 * @param sd
	 *            待添加的sd
	 * @return 若成功返回true，否则返回false
	 */
	@PUT
	@Path("/add")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public boolean addsd(Sd sd) {
		return server.addsd(sd);
	}
	
	@GET
	@Path("/info")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Dao>  getShapeById(@QueryParam("id") String id) {
		List<Dao> lists  = server.getShapeById(id);
		return lists;
	}
	@GET
	@Path("/tjjg")
	@Produces(MediaType.APPLICATION_JSON)
	public StatisticalResult getStatisticalResult(@QueryParam("szlx") String szlx,@QueryParam("xzqhbm") String xzqhbm,@QueryParam("type") String type){
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("lxbm", szlx);
		map.put("xzqhbm", xzqhbm);
		map.put("type", type);
		StatisticalResult tjjg = server.countByParameter(map);
		return tjjg;
	}
	/**
	 * 路线隧道
	 * @return
	 */
	@GET
	@Path("/sdNode")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Znode> tree(){
		return PreparedDataListener.getData("sd");
//		return server.sdNode();
	}
	
	@GET
	@Path("/detail")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Sd> detail(@QueryParam("szlx") String szlx,@QueryParam("xzqhbm") String xzqhbm,
			@QueryParam("sdfl")String sdfl,@QueryParam("pdfl")String pdfl){
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("lxbm", szlx);
		map.put("xzqhbm", xzqhbm);
		try{
			map.put("sdfl", Integer.parseInt(sdfl));
		}catch (Exception e){
			System.out.println("不是类型分类！");
		}
		try{
			map.put("pdfl", /*Integer.parseInt(pdfl)*/pdfl);
		}catch (Exception e){
			System.out.println("不是评定分类！");
		}
		List<Sd> detail = server.detail(map);
		return detail;
	}
	
	@GET
	@Path("/detail")
	@Produces(MediaType.APPLICATION_JSON)
	public EasyUI<Sd> detail(@QueryParam("szlx") String szlx,@QueryParam("xzqhbm") String xzqhbm,
			@QueryParam("sdfl")String sdfl,@QueryParam("pdfl")String pdfl,@QueryParam("page") int page,@QueryParam("rows") int rows){
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		EasyUI<Sd> easyUI = new EasyUI<Sd>(page, rows);
		Map<String, Object> map = easyUI.getParameter();
		map.put("lxbm", szlx);
		map.put("xzqhbm", xzqhbm);
		try{
			map.put("sdfl", Integer.parseInt(sdfl));
		}catch (Exception e){
			System.out.println("不是类型分类！");
		}
		try{
			map.put("pdfl", /*Integer.parseInt(pdfl)*/pdfl);
		}catch (Exception e){
			System.out.println("不是评定分类！");
		}
		List<Sd> detail = server.detail(map);
		int total = server.countDetail(map);
		easyUI.setRows(detail);
		easyUI.setTotal(total);
		return easyUI;
	}
	
    @GET
	@Path("/all")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Sd> all(@QueryParam("lxbm") String lxbm,@QueryParam("xzqhbm")String xzqhbm,
			@QueryParam("sxxfx") String sxxfx,@QueryParam("qdzh") String qdzh_string,
			@QueryParam("zdzh") String zdzh_string,@QueryParam("name") String name){
		Double qdzh = null;
		Double zdzh = null;
		try{
			qdzh = Double.parseDouble(qdzh_string);
			zdzh = Double.parseDouble(zdzh_string);
		}catch(Exception e){
			System.out.println("qdzh和zdzh没有输入或格式错误！");qdzh=null;zdzh=null;
		}
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		List<Sd> list = server.listAll(lxbm,xzqhbm,sxxfx,qdzh,zdzh,name);
		return list;
	}
	
	@GET
	@Path("/one")
	@Produces(MediaType.APPLICATION_JSON)
	public Sd one(@QueryParam("id")String id){
		Sd one = server.queryById(id);
		return one;
	}
	
	@GET
	@Path("/auto")
	@Produces(MediaType.APPLICATION_JSON)
	public List<String> pienext(@QueryParam("name")String name){
		return server.selectName(name);
	}
}
